package cn.hzu.service;

import cn.hzu.bean.*;
import cn.hzu.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {

    @Autowired(required = false)
    UserMapper userMapper;
    @Autowired(required = false)
    CourseMapper courseMapper;
    @Autowired(required =false)
    U_courseMapper u_courseMapper;
    @Autowired(required =false)
    Course_srcMapper course_srcMapper;
    @Autowired(required =false)
    course_noteMapper course_noteMapper;

    //删除笔记
    public void delete_note(int id){ course_noteMapper.delete_note(id);}
    public void update_id(int id){ course_noteMapper.update_id(id);}
    //遍历查找笔记
    public List<course_note> findnote(String email){ return course_noteMapper.findnote(email);}

    public String selectpswByemail(String email){
        return userMapper.selectpswByemail(email);
    }
    public int insertUser(User user){ return userMapper.insertUser(user);}
    public void updatepsw(String email, String password){ userMapper.updatepsw(email,password);}
    public void updatestate(String email){ userMapper.updatestate(email);}
    public void updatestate1(String email){ userMapper.updatestate1(email);}

    public String selectname(String email){ return userMapper.selectname(email);}
    public String selectsex(String email){ return userMapper.selectsex(email); }
    public String selectphone(String email){ return userMapper.selectphone(email);}
    public String selectnickname(String email){ return  userMapper.selectnickname(email); }

    //修改用户信息
    public void upadteuser(String email, String name, String sex, String phone, String nickname){
        userMapper.updateuser(email, name, sex, phone, nickname);
    }


    public String selectUser(){ return userMapper.selectUser();}
    public void updateall(){ userMapper.updateall();}

    public boolean login(User user){
        String email=user.getEmail();
        String password=user.getPsw();
        String psw=selectpswByemail(email);
        if(psw==null){
            return false;
        }else{
            if(psw.equals(password)) return true;
            else return false;
        }
    }

    public boolean register(User user){
        String emial=user.getEmail();
        String psw=selectpswByemail(emial);
        if(psw==null) {
            insertUser(user);
            return true;
        }
        else return false;

    }

//    public boolean updatepsw(User user){
//        String email=user.getEmail();
//        String psw=selectpswByemail(email);
//        if(psw==null) return false;
//        else {
//            updatepsw(email);
//            return false;
//        }
//    }

    public void updatestate(User user){
        String email=user.getEmail();
        updatestate(email);
    }
    public void updatestate1(User user) {
        String email = user.getEmail();
        updatestate1(email);
    }
    public List<User> findAllu(){
        List<User> users=userMapper.users();
        return users;
    }
    public List<course> findAllc(){
        List<course> courses=courseMapper.courses();
        return courses;
    }
    public List<course> findalluc(int u_id){
        List<U_course> u_courses=u_courseMapper.U_courses(u_id);
        List<course> ucourses=new ArrayList<>();
        for(int i=0;i<u_courses.size();i++){
            ucourses.add(courseMapper.uccourse(u_courses.get(i).getC_id()));
        }
        return ucourses;
    }
    public User findlogin(){
        User user=userMapper.login_user();
        return user;
    }
    public course findcourse(int id){
        course c=courseMapper.uccourse(id);
        return c;
    }
    public boolean add_ucourse(U_course u_course){
        if(u_courseMapper.find_ucourse(u_course)==null){
            u_courseMapper.add_ucourse(u_course);
            return true;
        }
        return false;
    }
    public void delete_ucourse(U_course u_course){
        u_courseMapper.delete_ucourse(u_course);
    }
    public int find_id(String name){
        return courseMapper.find_id(name);
    }
    public List<String> find(Course_src course_src){return course_srcMapper.find(course_src);}
    public List<String> find_ch(int id){return course_srcMapper.find_ch(id);}
    public int count(){return course_noteMapper.count();}
    public void add_note(course_note note){course_noteMapper.add_note(note);}
}
